﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using OhioState.AssetManaged;
using OhioState.Controls;
using OhioState.Graphics;
using OhioState.Controls.AssetMgr;

namespace OhioState.Controls
{
    public partial class FBOConfigControl : UserControl, IFrameBufferObject
    {
        public FBOConfigControl()
        {
            InitializeComponent();
            this.clearComboBox.SelectedIndex = 1;
            textureComboBoxes.Add(textureDropDownControl1);
            this.textureDropDownControl1.Init(typeof(ITexture));
            this.depthBufferDropDownControl.Init(typeof(IDepthBuffer));
        }

        private void addTextureButton_Click(object sender, EventArgs e)
        {
            textureTablePanel.SuspendLayout();
            int textureNum = textureTablePanel.RowCount;
            textureTablePanel.RowCount = textureNum + 1;
            Label label = new Label();
            // Table is not zero-based.
            label.Text = "Texture" + textureNum.ToString();
            label.Anchor = AnchorStyles.Top | AnchorStyles.Right;
            AssetMgr.AssetManagerDropDownControl textureDropDownControl = new AssetMgr.AssetManagerDropDownControl();
            textureComboBoxes.Add(textureDropDownControl);
            textureDropDownControl.Anchor = AnchorStyles.Top | AnchorStyles.Left;
            textureTablePanel.Controls.Add(label, 0, textureNum);
            textureTablePanel.Controls.Add(textureDropDownControl, 1, textureNum);
            //textureTablePanel.RowStyles.Add(new RowStyle(SizeType.Absolute,25));
            textureDropDownControl.Init(typeof(ITexture));
            textureTablePanel.ResumeLayout();
        }

        private void depthBufferCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (depthBufferCheckBox.Checked)
            {
                this.depthBufferDropDownControl.Enabled = true;
                this.newDepthBufferButton.Enabled = true;
            }
            else
            {
                this.depthBufferDropDownControl.Enabled = false;
                this.newDepthBufferButton.Enabled = false;
            }
        }

        private void newTextureButton_Click(object sender, EventArgs e)
        {
            // Show a new dialog for creating a texture (width, height, format, ...
            NewTextureDialog textureDialog = new NewTextureDialog();
            if (textureDialog.ShowDialog() == DialogResult.OK)
            {
                ITexture texture = textureDialog.CreateTexture();
                AssetManager.Instance.Add(texture as IAssetManaged);
            }
        }

        private void newDepthBufferButton_Click(object sender, EventArgs e)
        {
            // Show a new dialog for creating a depth buffer
            NewDepthBufferDialog depthBufferDialog = new NewDepthBufferDialog();
            if (depthBufferDialog.ShowDialog() == DialogResult.OK)
            {
                IDepthBuffer depthBuffer = depthBufferDialog.CreateDepthBuffer();
                AssetManager.Instance.Add(depthBuffer as IAssetManaged);
            }
        }

        #region IFrameBufferObject Members
        public IDepthBuffer DepthBuffer
        {
            get { return this.depthBufferDropDownControl.GetSelected() as IDepthBuffer; }
        }

        public FrameBufferClear ClearFlags
        {
            get
            {
                switch (this.clearComboBox.SelectedIndex)
                {
                    case 0:
                        return 0;
                    case 1:
                        return FrameBufferClear.Color;
                    case 2:
                        return FrameBufferClear.Depth;
                    case 3:
                        return (FrameBufferClear.Color | FrameBufferClear.Depth);
                }
                return 0;
            }
        }

        public void Disable(IRenderPanel panel)
        {
            throw new NotImplementedException();
        }

        public void Enable(IRenderPanel panel)
        {
            throw new NotImplementedException();
        }

        public IList<ITexture> Textures
        {
            get
            {
                IList<ITexture> textures = new List<ITexture>(textureComboBoxes.Count);
                foreach (AssetManagerDropDownControl control in textureComboBoxes)
                {
                    ITexture texture = control.GetSelected() as ITexture;
                    textures.Add(texture);
                }
                return textures;
            }
        }
        #endregion

        private IList<AssetManagerDropDownControl> textureComboBoxes = new List<AssetManagerDropDownControl>(8);
    }
}
